package com.xdwanj.smartcity.data.request;

import android.os.Environment;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;

import java.util.Map;

public class DownloadRequest extends Request<byte[]> {

    private final Response.Listener<byte[]> mListener;
    public Map<String, String> responseHeader;

    /**
     * @param method        请求类型
     * @param url           地址
     * @param listener      responseListener
     * @param errorListener errorListener
     */
    public DownloadRequest(int method, String url, Response.Listener<byte[]> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        // 关闭缓存
        setShouldCache(false);
        // 保存这个响应监听器
        mListener = listener;
    }

    /**
     * 解析网络响应
     *
     * @param networkResponse 网络响应
     * @return 响应
     */
    @Override
    protected Response<byte[]> parseNetworkResponse(NetworkResponse networkResponse) {
        // 响应头
        responseHeader = networkResponse.headers;
        return Response.success(networkResponse.data, HttpHeaderParser.parseCacheHeaders(networkResponse));
    }

    /**
     * 网络响应
     *
     * @param bytes 数据
     */
    @Override
    protected void deliverResponse(byte[] bytes) {
        mListener.onResponse(bytes);
    }

}
